<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">

<head>
    <th:block th:replace="include :: header('添加角色')" />
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>

<body>
    <div class="layui-form layuimini-form">
        <div class="layui-form-item" th:if="(${data})">
            <div class="layui-input-block" style="display: none;">
                <input type="text" name="id" th:value="${data.id}" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">角色名</label>
            <div class="layui-input-block">
                <input type="text" name="name" lay-verify="required" lay-reqtext="角色名不能为空" placeholder="请输入角色名" th:value="${data!=null?data.name:''}"
                    class="layui-input">
                <tip>填写角色名名称。</tip>
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">描述信息</label>
            <div class="layui-input-block">
                <textarea name="desc" th:text="${data!=null?data.desc:''}" class="layui-textarea" placeholder="请输入描述信息"></textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </div>
    <th:block th:replace="include :: common_js" />
    <script th:inline="none">
        layui.use(['form'], function () {
            var form = layui.form,
                layer = layui.layer,
                $ = layui.$;

            //监听提交
            form.on('submit(saveBtn)', function (data) {
                var index;

                var postUrl = ctx + 'system/role'
                $.ajax({
                    url: postUrl,
                    data: JSON.stringify(data.field),
                    cache: false,
                    contentType: "application/json",
                    type: 'POST',
                    timeout: 3000,
                    dataType: "json",
                    success: function (result) {
                        index = result.id
                    },
                    error: function () {
                        alert("post reqeust error");
                    }
                });

                // 关闭弹出层
                layer.close(index);

                var iframeIndex = parent.layer.getFrameIndex(window.name);
                parent.layer.close(iframeIndex);
                parent.location.reload();
                return true;
            });

        });
    </script>
</body>

</html>